package com.ruoyi.dl.controller.wechat;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.dl.domain.DlOrders;
import com.ruoyi.dl.domain.DlOrdersGoods;
import com.ruoyi.dl.domain.DlUser;
import com.ruoyi.dl.service.IDlOrdersGoodsService;
import com.ruoyi.dl.service.IDlOrdersService;
import com.ruoyi.dl.service.IDlUserService;
import com.ruoyi.dl.uitls.Tool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 订单Controller
 *
 * @author ruoyi
 * @date 2020-12-28
 */
@Controller
@RequestMapping("/wechat/dl/orders")
public class WechatDlOrdersController extends BaseController {

    @Autowired
    private IDlOrdersService dlOrdersService;
    @Autowired
    private IDlUserService dlUserService;
    @Autowired
    private IDlOrdersGoodsService dlOrdersGoodsService;


    /**
     * 查询订单列表
     */
    @PostMapping("/list")
    @ResponseBody
    @CrossOrigin
    public TableDataInfo list(DlOrders dlOrders) {
        startPage();
        List<DlOrders> list = dlOrdersService.mySelectDlOrdersList(dlOrders);
        return getDataTable(list);
    }


    /**
     * 新增保存订单
     */
    @Log(title = "订单", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    @CrossOrigin
    public AjaxResult addSave(DlOrders dlOrders) {
        try {
            if (dlOrders != null && dlOrders.getOrdersGoodsId() != null) {
                DlOrdersGoods dlOrdersGoods = dlOrdersGoodsService.selectDlOrdersGoodsById(dlOrders.getOrdersGoodsId());
                dlOrders.setCreateTime(new Date());
                dlOrders.setOrdersType(0);
                dlOrders.setOrdersNumber(Tool.generateUniqueKey("DL"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return toAjax(dlOrdersService.insertDlOrders(dlOrders));
    }


    /**
     * 删除订单
     */
    @Log(title = "订单", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    @CrossOrigin
    public AjaxResult remove(String ids) {
        return toAjax(dlOrdersService.deleteDlOrdersByIds(ids));
    }


    //验收
    //解除发单人和接单人冻结资金.扣钱加钱.判断是否扣除效率金
    @PostMapping("/acceptance")
    @ResponseBody
    public AjaxResult updateOrderById(Integer id) {
        DlOrders dlOrders = dlOrdersService.selectDlOrdersById(id);
        DlOrdersGoods dlOrdersGoods = dlOrdersGoodsService.selectDlOrdersGoodsById(dlOrders.getOrdersGoodsId());
        Date createTime = dlOrders.getCreateTime();
        Calendar c = Calendar.getInstance();
        c.setTime(createTime);
        c.add(Calendar.HOUR_OF_DAY, Integer.parseInt(dlOrdersGoods.getTimeLimit()));
        Date xdtime = c.getTime();
        DlUser fdUser = dlUserService.selectDlUserById(dlOrdersGoods.getUserId());
        DlUser jdUser = dlUserService.selectDlUserById(dlOrders.getUserId());
        if (fdUser != null && jdUser != null) {
            if (fdUser.getBalance().compareTo(dlOrdersGoods.getOrderPrice()) == -1) {
                return AjaxResult.error("余额不足请充值！！", 2000);
            } else {
                fdUser.setFreeze(fdUser.getFreeze().subtract(dlOrdersGoods.getOrderPrice()));
//                fdUser.setBalance(fdUser.getBalance().subtract(dlOrdersGoods.getOrderPrice()));
                dlUserService.updateDlUser(fdUser);
                if (xdtime.compareTo(dlOrders.getFinishTime()) == -1) {
                    jdUser.setBalance(jdUser.getBalance().add(dlOrdersGoods.getOrderPrice().
                            subtract(dlOrdersGoods.getXlMoney().add(dlOrdersGoods.getBzMoney()))).add(dlOrdersGoods.getXlMoney().add(dlOrdersGoods.getBzMoney())));
                } else {
                    jdUser.setBalance(jdUser.getBalance().add(dlOrdersGoods.getOrderPrice())
                            .add(dlOrdersGoods.getXlMoney().add(dlOrdersGoods.getBzMoney())));
                }
                jdUser.setFreeze(jdUser.getFreeze().subtract(dlOrdersGoods.getBzMoney().add(dlOrdersGoods.getXlMoney())));
                dlUserService.updateDlUser(jdUser);
                dlOrders.setOrdersType(4);
            }
        }
        return toAjax(dlOrdersService.updateDlOrders(dlOrders));
    }

    /**
     * 根据已结商单id 更改状态
     *
     * @param billId 商单id
     * @return
     */
    @PostMapping("/updateOrderGoodsOrdersTypeById")
    @ResponseBody
    public AjaxResult updateOrderGoodsOrdersTypeById(Integer billId) {
        DlOrders dlOrders = new DlOrders();
        dlOrders.setId(billId);
        dlOrders.setOrdersType(3);
        dlOrders.setFinishTime(DateUtils.getNowDate());
        return toAjax(dlOrdersService.updateDlOrders(dlOrders));
    }
}
